import { Tree, createTreeFactory, INodeStruct, flatTree } from 'spd-components/tree';
import { OriganizationUnit, IEnterpriseStruct } from 'spd-request/organization-unit';
import { useState } from 'react';
import { message } from 'antd';
// @ts-ignore
export const createTree = createTreeFactory<IEnterpriseStruct>('parentId', 'id', 'parentId',  (data) => data.displayName );
export function useGetTreeList(): [INodeStruct<IEnterpriseStruct>[], (isNewData?: boolean) => void, IEnterpriseStruct[]] {
    let [tree, setTree] = useState<INodeStruct<IEnterpriseStruct>[]>([]);
    let [originData, setOriginData] = useState<IEnterpriseStruct[]>([]);
    function getList(isNewData: boolean = true) {
        if(isNewData == true) {
            OriganizationUnit.getEnterpriseOrganizationUnits().then(({error, data}) => {
                if(error) {
                    return message.error(error);
                }
                let treeData: INodeStruct<IEnterpriseStruct>[] = createTree(null, data.items);
                setOriginData(data.items);
                setTree(treeData);
            });
        } else {
            setTree([...tree]);
        }
    }
    return [tree, getList, originData]
}